package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.fanxiuspring.FanXiuSpringConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.fanxiuspring.FanXiuSpringMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class FanXiuSpringRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        FanXiuSpringConfig config = FanXiuSpringMgr.getActivityConfig(FanXiuSpringMgr.CHILD_TYPE_OTHER);
        if(config != null){
            //不转积分
            //addRecycleItemData(config.getActivityInfo(), eItemId.FAN_XIU_SPRING_DRAW_ITEM.getValue(), 1, config.getScoreItemId());
            addRecycleItemData(config.getActivityInfo(), eItemId.FAN_XIU_SPRING_SHOP_ITEM_1.getValue(), 2, 0);
            addRecycleItemData(config.getActivityInfo(), eItemId.FAN_XIU_SPRING_SHOP_ITEM_2.getValue(), 2, 0);
            addRecycleItemData(config.getActivityInfo(), config.getPiceItemId(), 2, 0);
            //转道具
            List<String> paramList = new ArrayList<>();
            long recycleTime = config.getActivityInfo().getEndShowTime() * 1000L + 600 * 1000;
            paramList.add(String.valueOf(eItemId.FAN_XIU_SPRING_DRAW_ITEM.getValue()));
            paramList.add(config.getItemRecycle());//目标道具
            paramList.add(config.getActivityInfo().getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(eItemId.FAN_XIU_SPRING_DRAW_ITEM.getValue(), activityType, 3, recycleTime, paramList);
        }
    }

    private void addRecycleItemData(ActivityInfo activityInfo, int itemId, int recycleType, int scoreItemId) {
        List<String> paramList = new ArrayList<>();
        long recycleTime = activityInfo.getEndShowTime() * 1000L + 600 * 1000;
        paramList.add(String.valueOf(itemId));
        paramList.add(String.valueOf(scoreItemId));
        paramList.add("0");
        paramList.add(activityInfo.getActivityName());
        ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, recycleType, recycleTime, paramList);
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.FanXiuSpringActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        if(data.getRecycleType() == 1){
            String titleKey = MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE;
            String contentKey = MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT;
            item2Score(userId, data, titleKey, contentKey);
        }else if(data.getRecycleType() == 2){
            int itemId = data.getItemId();
            UserBagItem bagItem = getUserBagItem(userId, itemId);
            removeItem(bagItem);
        }else if(data.getRecycleType() == 3){
            String language = UserMgr.getUserInfo(userId).getLanguage();
            UserBagItem userBagItem = getUserBagItem(userId, data.getItemId());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                // 转换其他道具
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(data.getItemId());
                if(goodsInfo != null){
                    long num = userBagItem.getNum();
                    if (removeItem(userBagItem)){
                        Property reward = PropertyHelper.parseStringToProperty(data.getParamList().get(1));
                        reward.rideProperty(num);
                        //  发邮件
                        String removeName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                        String activityName = ServerLanguageMgr.getContent(data.getParamList().get(2), language);
                        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_TITLE, language), activityName);
                        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_RECYCLE_REMOVE_ITEM_CONTENT,
                                language), removeName, num);
                        MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
                    }
                }
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
